<template>
  <div>
    <!-- 部门管理 -->

    <!-- 表单 -->
    <el-form size="small" :model="form" :inline="true">
      <el-form-item label="部门名称" style="font-weight:bold;">
        <el-select v-model="form.departName" placeholder="请选择部门名称" style="width:200px;" clearable>
          <el-option v-for="item in DepartmentName" :key="item.id" :label="item.departName"
            :value="item.departName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状 态" style="font-weight:bold;">
        <el-select v-model="form.state" placeholder="请选择状态" style="width:200px;" clearable>
          <el-option label="正常" value="0"></el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-button size="small" class="newly_button" type="primary" icon="el-icon-folder-add"
        @click="dialog_newly = true">新增</el-button>
      <el-button size="small" class="search_button" type="primary" icon="el-icon-search"
        @click="getDataList">查询</el-button>
      <el-button size="small" class="reset_button" icon="el-icon-refresh" @click="reset">重置</el-button>
    </el-form>
    <!-- 表单 -->
    <el-row>
      <!-- <el-button size="small" class="newly_button" type="primary" icon="el-icon-folder-add" @click="dialog_newly = true">新增</el-button> -->
      <!-- <el-button type="danger" icon="el-icon-delete">删除</el-button> -->
    </el-row>

    <el-divider></el-divider>
    <!-- 表格 -->
    <el-table size="medium" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange" border>
      <!-- <el-table-column type="selection" width="50" align="center"></el-table-column> -->
      <!-- <el-table-column prop="id" label="ID" align="center"></el-table-column> -->
      <el-table-column prop="departName" label="部门名称" align="center"></el-table-column>
      <!-- <el-table-column prop="departType" label="部门类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.departType == 0">学校部门</span>
          <span v-if="scope.row.departType == 1">物业部门</span>
        </template>
      </el-table-column> -->
      <el-table-column prop="state" label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 0" style="color:#67C23A;">正常</span>
          <span v-if="scope.row.state == 1" style="color:red;">停用</span>
        </template>
      </el-table-column>
      <el-table-column prop="handle" label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="updateDepartment(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" plain @click="deleteDepartment({ id: scope.row.id })">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination class="pagination" background layout="prev, pager, next" :total="total" :page-size="10"
      @current-change="handleCurrentChange"></el-pagination>

    <!-- 添加部门弹窗 -->
    <el-dialog title="添加部门" :visible.sync="dialog_newly">
      <el-form :model="formNew" :label-position="labelPosition" label-width="100px" style="margin-left:20%;">
        <el-form-item label="部门名称 :">
          <el-input v-model="formNew.departName" style="width:60%" placeholder="请输入部门名称" clearable></el-input>
        </el-form-item>
        <!-- <el-form-item label="部门类型">
          <el-select v-model="formNew.departType" placeholder="请选择部门类型" style="width:60%">
            <el-option label="学校部门" value="0"></el-option>
            <el-option label="物业部门" value="1"></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="状态">
          <el-select v-model="formNew.state" placeholder="请选择状态" style="width:60%">
            <el-option label="正常" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_newly = false">取 消</el-button>
        <el-button type="primary" @click="addDepartment" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加部门弹窗 -->

    <!-- 修改部门信息弹窗 -->
    <el-dialog title="修改部门信息" :visible.sync="dialog_change">
      <el-form :model="formChange" :label-position="labelPosition" label-width="100px" style="margin-left:20%;">
        <el-form-item label="部门名称 :">
          <el-input v-model="formChange.departName" style="width:60%" placeholder="请输入部门名称" clearable></el-input>
        </el-form-item>
        <!-- <el-form-item label="部门类型">
          <el-select v-model="formChange.departType" placeholder="请选择部门类型" style="width:60%">
            <el-option label="学校部门" value="0"></el-option>
            <el-option label="物业部门" value="1"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="状态">
          <el-select v-model="formChange.state" placeholder="请选择状态" style="width:60%">
            <el-option label="正常" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_change = false">取 消</el-button>
        <el-button type="primary" @click="updateDepartmentConfirm" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改部门信息弹窗 -->
  </div>
</template>

<script>
import { getDepartmentName, getDepartmentList, addDepartment, updateDepartment, deleteDepartment } from '_api/DormManage/Department/index.js'
export default {
  data() {
    return {
      dialog_newly: false,
      dialog_change: false,
      labelPosition: "right",
      form: {
        current: 0,
        departName: "",
        field: "",
        order: "",
        pageSize: 10
      },
      formNew: {
        state: 0
      },
      formChange: {},
      DepartmentName: [],
      tableData: [],
      total: 0
    }
  },
  methods: {
    handleSelectionChange() {

    },
    //重置
    reset() {
      this.$router.go(0)
    },
    //删除部门
    deleteDepartment(e) {
      this.$confirm('此操作将删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDepartment(e).then(res => {
          console.log(res)
          if (res.status == 200) {
            this.$message({
              message: '删除成功!',
              duration: 1000,
              type: 'success',
              onClose: () => {
                this.$router.go(0)
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    //修改部门弹窗
    updateDepartment(e) {
      this.dialog_change = true
      this.formChange = e
      this.formChange.state = e.state.toString()
      this.formChange.departType = e.departType.toString()
      console.log(e)
    },
    //修改部门确认
    updateDepartmentConfirm() {
      updateDepartment(this.formChange).then(res => {
        console.log(res)
        if (res.status == 200) {
          this.dialog_change = false
          this.$message({
            message: '修改成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.$router.go(0)
            }
          })
        }
      })
    },
    // 添加部门
    addDepartment() {
      addDepartment(this.formNew).then(res => {
        console.log(res)
        if (res.status == 200) {
          this.dialog_newly = false
          this.$message({
            message: '添加成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.$router.go(0)
            }
          })
        }
      })
    },
    // prev(e) {
    //   //上一页
    //   this.form.current = e-1
    //   this.getDepartmentList()
    // },
    // next(e) {
    //   //下一页
    //   this.form.current = e-1
    //   this.getDepartmentList()
    // },
    handleCurrentChange(e) {
      //当前页
      this.form.current = e
      this.getDepartmentList()
    },
    // 查询按钮
    getDataList() {
      this.form.current = 0
      this.getDepartmentList()
    },
    // 查询部门信息
    getDepartmentList() {
      getDepartmentList(
        this.form
      ).then(res => {
        console.log(res)
        if (res.status == 200) {
          this.getDepartmentName()
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        }
      }).catch(error => {
        // console.log(error.response.status);
        // if (error.response.status == 401) {
        //   this.$confirm('登录状态过期, 请重新登录!', '提示', {
        //     confirmButtonText: '确定',
        //     showCancelButton: false,
        //     type: 'warning'
        //   }).then(() => {
        //     this.$router.push('/login')
        //   }).catch(() => {

        //   })
        // }
      })
    },
    //获取部门名称
    getDepartmentName() {
      getDepartmentName({}).then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.DepartmentName = res.data.data
        }
      })
    }
  },
  created() {
    this.getDepartmentList()
    // this.getDepartmentName()
  },
};
</script>
<style lang="scss" scoped>
.search_button {
  margin-left: 80px;
}

.reset_button {
  margin-left: 10px;
}

.newly_button {
  margin-left: 30px;
}

.pagination {
  float: right;
  margin-top: 10px;
}
</style>
